package com.wenjh.jdmzt.util;

import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.text.Editable;
import android.text.Selection;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.wenjh.jdmzt.R;


public class ViewUtil {

	public static void setLinkStyle(TextView textView, int start, int end) {
		textView.setFocusable(true);
		textView.setClickable(true);
		SpannableStringBuilder builder = new SpannableStringBuilder(
				textView.getText());
		builder.setSpan(new TextAppearanceSpan(textView.getContext(),
				R.style.TextLinked), start, end,
				Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		builder.setSpan(new UnderlineSpan(), start, end,
				Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		textView.setText(builder);
	}
	

	/** 把光标移动到EditText的最前边 */
	public static void moveCursor(EditText et) {
		Editable etable = et.getText();
		Selection.setSelection(etable, 0);
		etable.clear();
	}

	
	/**
	 * 对控件截图。
	 * 
	 * @param v
	 *            需要进行截图的控件。
	 * @param quality
	 *            图片的质量
	 * @return 该控件截图的byte数组对象。
	 */
	public static byte[] printScreen(View v, int quality) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		v.setDrawingCacheEnabled(true);
		v.buildDrawingCache(true);
		Bitmap bitmap = v.getDrawingCache();
		bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
		return baos.toByteArray();
	}

	/**
	 * 截图
	 * 
	 * @param v
	 *            需要进行截图的控件
	 * @return 该控件截图的Bitmap对象。
	 */
	public static Bitmap printScreen(View v) {
		v.setDrawingCacheEnabled(true);
		v.buildDrawingCache();
		return v.getDrawingCache();
	}
	
}
